import useCommonUtils from './common'
const { formatDate } = useCommonUtils();

const calculateNextPeriod = (startTime, circleMonthTime, diffMonth = 0) => {
  const startDate = new Date(startTime); // 将字符串转换为日期对象
  const currentDate = new Date(); // 获取当前日期

  // 计算从开始日期到当前日期的总月数
  const monthsSinceStart = (currentDate.getFullYear() - startDate.getFullYear()) * 12 + (currentDate.getMonth() - startDate.getMonth());

  // 计算距离最近的周期结束的月数
  let monthsToNextPeriod;
  if (monthsSinceStart % circleMonthTime === 0) monthsToNextPeriod = circleMonthTime; // 如果正好在周期结束时，直接进入下一个周期
  else monthsToNextPeriod = circleMonthTime - (monthsSinceStart % circleMonthTime); // 否则，计算到下一个周期结束的月数

  // 创建下一个周期结束的日期对象
  const nextPeriodEndDate = new Date(currentDate.getFullYear() + Math.floor((currentDate.getMonth() + monthsToNextPeriod) / 12), ((currentDate.getMonth() + monthsToNextPeriod) % 12) % 12 + diffMonth, startDate.getDate());

  // return formatDate(nextPeriodEndDate)// 返回结果, 只保留日期部分
  return nextPeriodEndDate
}

// const isWithinTimeRange = (timeStr, diffMounthTime) => {
//   const time = new Date(timeStr); // 将时间字符串转换为Date对象
//   const offsetTime = new Date(time.setMonth(time.getMonth() + diffMounthTime));  // 计算偏移后的日期
//   const now = new Date();  // 获取当前时间
//   // 判断当前时间是否在time和offsetTime之间
//   if (diffMounthTime >= 0) return now >= time && now <= offsetTime;
//   else return now >= offsetTime && now <= time;
// }

const useCar = () => {
  const calculateCarCheckStatus = (registerTime, lastCheckTime) => {
    const diffMonth = 3; //截止日期前，3个月内办理下一次的车检
    const nowDate = new Date(); // 获取当前日期
    const lastCheckDate = new Date(lastCheckTime); // 获取当前日期
    const registerDate = new Date(registerTime); // 将字符串转换为日期对象
    const exemptCheckDate = new Date(registerDate.getFullYear() + 6, registerDate.getMonth(), registerDate.getDate() + 1);
    const perYearCheckDate = new Date(registerDate.getFullYear() + 15, registerDate.getMonth(), registerDate.getDate() + 1);

    let checkStatus; // 0未年检(年检超期) 1已年检
    let exemptStatus; // 0需年检 1免年检
    let nextCheckTime;

    let period;
    const isExempt = nowDate < exemptCheckDate;
    const isBetween6And15 = nowDate >= exemptCheckDate && nowDate <= perYearCheckDate;
    // const isOver15 = nowDate > perYearCheckDate;

    if (isExempt) period = 2 * 12;
    else if (isBetween6And15) period = 1 * 12;
    else period = 0.5 * 12;

    const nextPeriodDate = calculateNextPeriod(registerTime, period);
    const nextPeriodDiffDate = new Date(nextPeriodDate.getFullYear(), nextPeriodDate.getMonth() - diffMonth, nextPeriodDate.getDate())
    const lastPeriodDate = new Date(nextPeriodDate.getFullYear() - period / 12, nextPeriodDate.getMonth(), nextPeriodDate.getDate())
    const lastPeriodDiffDate = new Date(nextPeriodDate.getFullYear() - period / 12, nextPeriodDate.getMonth() - diffMonth, nextPeriodDate.getDate())

    // console.log(isExempt, isBetween6And15);
    // console.log(formatDate(nowDate), 'nowDate');
    // console.log(formatDate(lastCheckDate), 'lastCheckDate');
    // console.log(formatDate(nextPeriodDate), 'nextPeriodDate');
    // console.log(formatDate(nextPeriodDiffDate), 'nextPeriodDiffDate');
    // console.log(formatDate(lastPeriodDate), 'lastPeriodDate');
    // console.log(formatDate(lastPeriodDiffDate), 'lastPeriodDiffDate');

    if (nowDate >= lastPeriodDate && nowDate < nextPeriodDiffDate) {
      // console.log('a');
      if (lastCheckDate < lastPeriodDiffDate) {
        checkStatus = 0
        exemptStatus = isExempt ? 1 : 0
        nextCheckTime = ''
        // console.log('1');
      }
      if (lastCheckDate >= lastPeriodDiffDate && lastCheckDate <= nowDate) {
        checkStatus = 1
        exemptStatus = isExempt ? 1 : 0
        nextCheckTime = formatDate(nextPeriodDate);
        // console.log('2');
      }
    }
    if (nowDate >= nextPeriodDiffDate && nowDate < nextPeriodDate) {
      // console.log('b');
      if (lastCheckDate < lastPeriodDiffDate) {
        checkStatus = 0
        exemptStatus = isExempt ? 1 : 0
        nextCheckTime = '';
        // console.log('3');
      }
      if (lastCheckDate >= lastPeriodDiffDate && lastCheckDate < nextPeriodDiffDate) {
        checkStatus = 1
        exemptStatus = isExempt ? 1 : 0
        nextCheckTime = formatDate(nextPeriodDate);
        // console.log('4');
      }
      if (lastCheckDate >= nextPeriodDiffDate && lastCheckDate < nextPeriodDate) {
        checkStatus = 1
        exemptStatus = isExempt ? 1 : 0
        nextCheckTime = formatDate(calculateNextPeriod(registerTime, period, period)); //再下一次的时间
        // console.log('5');
      }
    }

    // console.log({ checkStatus, exemptStatus, nextCheckTime });
    return { checkStatus, exemptStatus, nextCheckTime }
  }
  
  // const calculateCarCheckStatus2 = (registerTime, lastCheckTime) => {
  //   const diffMonth = 3; //截止日期前，3个月内办理下一次的车检
  //   const now = new Date(); // 获取当前日期
  //   const registerDate = new Date(registerTime); // 将字符串转换为日期对象
  //   const lastCheckDate = new Date(lastCheckTime); // 将字符串转换为日期对象
  
  //   // 计算车辆注册年份和当前年份
  //   const currentYear = now.getFullYear();
  //   const registerYear = registerDate.getFullYear();
    
  //   // 计算车辆的使用年数
  //   const yearsSinceRegistration = currentYear - registerYear;
  
  //   // 计算年检状态
  //   let status;
  //   let isNeedCheck;
  //   let nextCheckTime;
  //   if (yearsSinceRegistration <= 6) {
  //     status = '免上线检测';
  //     // nextCheckTime = new Date(registerYear + 6, registerDate.getMonth(), registerDate.getDate()).toISOString().split('T')[0];
  //     nextCheckTime = calculateNextPeriod(registerTime, 2 * 12);
  //     isNeedCheck = false; // 免检，不需要年检
  //   } else if (yearsSinceRegistration > 6 && yearsSinceRegistration <= 15) {
  //     const period = 1 * 12;
  //     status = '每年一次检测';
  //     nextCheckTime = calculateNextPeriod(registerTime, period, diffMonth);
  //     // lastCheckDate
  //     isNeedCheck = !isWithinTimeRange(nextCheckTime, -period); // 如果当前时间在下一次年检时间的前一年内，说明上次年检未完成
  //   }  else {
  //     const time = calculateNextPeriod(registerTime, 6, diffMonth);
  //     status = '每年两次检测';
  //     nextCheckTime = time;
  //     isNeedCheck = isWithinTimeRange(time, -diffMonth); // 需要年检
  //   }
  
  //   // 返回结果
  //   return {
  //     status,
  //     isNeedCheck,
  //     nextCheckTime,
  //     nextQualifiedMarkTime
  //   };
  // }

  return {
    calculateCarCheckStatus
  }
}

export default useCar;